<template>
  <a-modal :title="title" :width="640" :visible="visible" :confirmLoading="loading" @ok="handleSubmit" @cancel="()=>{this.visible=false}">
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="订单号" prop="PlanId">
          <a-input v-model="entity.PlanId" autocomplete="off" aria-readonly="true" disabled="disabled"/>
        </a-form-model-item>
        <a-form-model-item label="工序号" prop="OprSequenceNo">
          <a-input v-model="entity.OprSequenceNo" autocomplete="off" aria-readonly="true" disabled="disabled"/>
        </a-form-model-item>
        <a-form-model-item label="工序名称" prop="OprSequenceName">
          <a-input v-model="entity.OprSequenceName" autocomplete="off" aria-readonly="true" disabled="disabled"/>
        </a-form-model-item>
        <a-form-model-item label="工作中心" prop="WorkCenterName">
          <a-input v-model="entity.WorkCenterName" autocomplete="off" aria-readonly="true" disabled="disabled"/>
        </a-form-model-item>
        <a-form-model-item label="原工位" prop="WorkStationName">
          <a-input v-model="entity.WorkStationName" autocomplete="off" aria-readonly="true" disabled="disabled" />
        </a-form-model-item>
        <a-form-model-item label="改派工位" prop="WorkStation">
          <CommonSelect table="WorkshoStation" v-model="WorkStation.Id" :search="{ UnitId: entity.WorkCenter }"></CommonSelect>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { mapActions } from 'vuex'
import MainSvc from '@/api/Exec/OprSequenceSvc'
import EnumSelect from '@/components/CF/EnumSelect'
import CodeInput from '@/components/CF/CodeInput'
import TreeSelect from '@/components/CF/TreeSelect'
import CommonSelect from '@/components/CF/CommonSelect'
export default {
  components: {
    MainSvc,
    CodeInput,
    EnumSelect,
    TreeSelect,
    CommonSelect
  },
  props: {},
  data() {
    return {
      title: '新建',
      layout: { labelCol: { xs: { span: 24 }, sm: { span: 6 } }, wrapperCol: { xs: { span: 24 }, sm: { span: 14 } } },
      rules: {
        Name: [{ required: true, message: '必填' }],
        Code: [{ required: true, message: '必填' }]
      },
      visible: false,
      loading: false,
      entity: { WorkCenter: '' },
      WorkStation: { Id: '', Name: '' }
    }
  },
  computed: {
  },
  created() { },
  methods: {
    ...mapActions({ getConfig: 'getConfig' }),
    init() {
      this.loading = false
      this.visible = true
      this.entity = { Id: '', Code: '', Name: '', WorkCenter: '' }
      this.WorkStation = { Id: '', Name: '' }
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    openForm(id, uid, title) {
      this.entity.WorkCenter = uid
      this.title = title || (id ? '新建' : '修改')
      this.init()
      if (id) {
        MainSvc.Get(id).then(resJson => {
          this.entity = resJson.Data
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }

        if (this.entity.WorkStation === this.WorkStation.Id) {
          this.$message.error('改派工位不能为当前工位')
          return
        }

        this.entity.WorkStation = this.WorkStation.Id
        this.loading = true
        MainSvc.Save(this.entity).then(result => {
          this.loading = false
          if (result.Success) {
            this.$message.success(result.Msg)
            this.visible = false
            this.$emit('Success')
          } else {
            this.$message.error(result.Msg)
          }
        })
      })
    }
  }
}
</script>
